
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class DataAndTimePickers extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => DataAndTimePickersState();
}

class DataAndTimePickersState extends State<DataAndTimePickers> {
  var _data;
  var _timer;

  _showDataPicker() async {
    Locale myLocale = Localizations.localeOf(context);
    var picker = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2016),
        lastDate: DateTime(DateTime.now().year+1),
        locale: myLocale);
    setState(() {
      _data = picker.toString();
    });
  }

  _showTimePicker() async {
    var picker =
    await showTimePicker(context: context, initialTime: TimeOfDay.now());
    setState(() {
      _timer = picker.toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('时间选择'),
        centerTitle: true,
      ),
      body: Column(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          RaisedButton(
              child: Text(_data == null ? '选择日期' : _data),
              onPressed: () => {
                    _showDataPicker(),
                  }),
          RaisedButton(
              child: Text(_timer == null ? '选择时间' : _timer),
              onPressed: () => {
                    _showTimePicker(),
                  }),
        ],
      ),
    );
  }
}
